package cn.ly.security.controller;

import cn.ly.security.dao.UserMapper;
import cn.ly.security.entity.History;
import cn.ly.security.entity.Info;
import cn.ly.security.entity.User;
import cn.ly.security.service.impl.HistoryServiceImpl;
import cn.ly.security.service.impl.InfoServiceImpl;
import cn.ly.security.service.impl.UserServiceImpl;
import cn.ly.security.util.DataPojo;
import cn.ly.security.util.Pagely;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/History")
@CrossOrigin("*")
@Api(tags="维护历史")
public class HistoryController {
    @Autowired
    HistoryServiceImpl historyService;
    @Autowired
    UserServiceImpl userService;
    @Autowired
    InfoServiceImpl infoService;

    @GetMapping("/selectAllHistory")
    public DataPojo selectAllHistory(Pagely pagely){
        Page<History> page = new Page<>(pagely.getCurrent(),pagely.getSize());
        page.setTotal(historyService.count());
        List<History> histories = historyService.selectAllHistory(pagely.getCurrent(),pagely.getSize());
        ArrayList<Object> objects=new ArrayList<>();
        page.setRecords(histories);
        objects.add(page);
        return new DataPojo("200","success",objects);
    }
    @PutMapping("/insertHistory")
    public DataPojo insertHistory(History history, User user , Info info){
        QueryWrapper<User> userQueryWrapper=new QueryWrapper<>();
            userQueryWrapper.eq("u_username",user.getUUsername());
        User one = userService.getOne(userQueryWrapper);
        Info one1 = infoService.selectSingleDeviceByName(info.getIName());

//        QueryWrapper<Info> infoQueryWrapper =new QueryWrapper<>();
//        infoQueryWrapper.eq("i_name",info.getIName());
//        Info one1 = infoService.getOne(infoQueryWrapper);
        History history1 = new History();
        history1.setHUserId(one.getUId());
        history1.setHDeviceId(one1.getIId());
        history1.setHFaultStatus(one1.getIStage());
//        wait update
        history1.setHRepairTime(one1.getIRepairTime());
        history1.setHDescription(history.getHDescription());
        boolean save = historyService.save(history1);

        ArrayList<Object> objects =new ArrayList<>();
        objects.add(save);
        return new DataPojo("200","success",objects);


    }


}
